package com.maycari.captainbi;

import com.alibaba.excel.EasyExcel;
import com.maycari.tool.utils.StringUtil;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Author: 王宏伟
 * @DateTime: 2021/4/1 13:59
 * @Description: TODO
 */
public class create {

    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\excel.xlsx");
        List<Map<String, String>> objects = EasyExcel.read(file).headRowNumber(0).doReadAllSync();

        StringBuilder sb = new StringBuilder("CREATE TABLE `captain_17` ( \n");
        boolean hasId = false;
        for (Map<String, String> map : objects) {
            String k = map.get(0).toLowerCase();
            String v = map.get(1);
            String i = map.get(2);
            String c = map.get(3);
            sb.append("`" + k + "` ");
            if (k.equals("id")) {
                sb.append("bigint(20) ");
            } else if (StringUtil.isBlank(i)) {
                sb.append("int(11) ");
            } else {
                if (i.equals("v")) {
                    String a = StringUtil.isBlank(c) ? "100" : c;
                    sb.append("varchar(" + a + ")");
                } else if (i.equals("d")) {
                    sb.append("datetime ");
                } else if (i.equals("b")) {
                    sb.append("bigint(20)");
                } else {
                    if (i.equals("10")) {
                        sb.append("decimal(20," + i + ")");
                    } else {
                        sb.append("decimal(11," + i + ")");
                    }

                }
            }

            if (k.equals("id")) {
                hasId = true;
                sb.append("NOT NULL");
            } else {
                sb.append("DEFAULT NULL");
            }
            if (StringUtil.isNotBlank(v)) {
                sb.append(" COMMENT '" + v + "'");
            }
            sb.append(",\n");
        }

        if (hasId) {
            sb.append("  PRIMARY KEY (`id`) \n");
        }
        sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
        System.out.println(sb);
    }
}
